package com.dsg.controller.setting;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.constants.CommonConstants;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.security.SystemAuthorizingUser;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.setting.SsoRole;
import com.dsg.entity.setting.SsoRoleUser;
import com.dsg.service.setting.ISsoRoleService;
import com.dsg.service.setting.ISsoRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位（角色）信息表 前端控制器
 * </p>
 *
 * @author
 * @since 2017-10-30
 */
@RestController
@RequestMapping("/setting/ssoRole")
public class SsoRoleController extends BaseController {

    @Autowired
    private ISsoRoleService ssoRoleService;
    @Autowired
    private ISsoRoleUserService ssoRoleUserService;
    @Autowired
    private RedisTemplate redisTemplate;
    // @Autowired
    // private ISsoRoleFuncService ssoRoleFuncService;

    /**
     * 查询角色列表
     *
     * @param ssoRole
     * @param pageSize
     * @param pageNumber
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)

    public AjaxResult list(SsoRole ssoRole, int pageSize, int pageNumber) {
        AjaxResult rs = null;
        SystemAuthorizingUser sysUser = SingletonLoginUtils.getSystemUser();
        com.powerbridge.core.security.SystemAuthorizingUser systemUser = com.powerbridge.core.util.SingletonLoginUtils
                .getSystemUser(redisTemplate);
        try {
            // 如果systemUser用户不为空，则代表是userToken登录的用户
            if (systemUser != null && systemUser.getUserName() != null) {
                ssoRole.setOrgCode(systemUser.getOrgCode());
                ssoRole.setUserName(systemUser.getUserName());
            } else {
                if (CommonConstants.USER_TYPE_COM.equals(sysUser.getUserType())) {
                    return null;
                } else if (CommonConstants.USER_TYPE_ORG.equals(sysUser.getUserType())) {
                    ssoRole.setOrgCode(sysUser.getOrgCode());
                }
            }
            // 计算当前页码，实例化page
            Page page = new Page(pageNumber / pageSize + 1, pageSize);
            String sort = getParameter("sort");
            boolean sortOrder = getOrderSort(getParameter("sortOrder"));
//			if (StringUtil.isNotEmpty(sort)) {
//				page.setAsc(sortOrder);
//				page.setOrderByField(sort); // 排序
//			} else {
//				page.setOrderByField("ORDER_INDEX");
//			}
            ssoRoleService.selectByPage(page, ssoRole);
            rs = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            rs = setErrorJson(e.toString());
        }
        return rs;
    }

    /**
     * 添加角色信息
     *
     * @param ssoRole
     * @return
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public AjaxResult addEntity(@RequestBody SsoRole ssoRole) {
        AjaxResult rs = null;
        SystemAuthorizingUser systemUser = SingletonLoginUtils.getSystemUser();
        ssoRole.setOpUser(systemUser.getUserName());
        ssoRole.setOpTime(new Date());
        ssoRole.setOid(UUIDGenerator.getUUID());
        boolean flag = ssoRoleService.save(ssoRole);
        if (flag) {
            rs = json(MessageConstants.SSO_STATUS_SUCCESS, ssoRole);
        } else {
            rs = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return rs;
    }

    /**
     * 查阅角色信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/view", method = RequestMethod.POST)

    public AjaxResult viewEntity(@PathVariable String id) {
        AjaxResult rs = null;
        try {
            SsoRole ssoRole = ssoRoleService.getById(id);
            rs = json(MessageConstants.SSO_STATUS_SUCCESS, ssoRole);
        } catch (Exception e) {
            e.printStackTrace();
            rs = setErrorJson(e.toString());
        }
        return rs;
    }

    /**
     * 更新角色信息
     *
     * @param ssoRole
     * @return
     */
    @PostMapping(value = "/update")
    @ResponseBody
    public AjaxResult updateEntity(@RequestBody SsoRole ssoRole) {
        AjaxResult rs = null;
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("ROLE_OID", ssoRole.getOid());
            // List<SsoRoleFunc> list = ssoRoleFuncService.listByMap(params);
            // if(null != list && list.size() > 0)
            // rs = setJson(MessageConstants.SSO_STATUS_FAIL.getCode(), "角色已经被授权，无法修改！");
            // else {
            boolean flag = ssoRoleService.updateById(ssoRole);
            if (flag) {
                rs = json(MessageConstants.SSO_STATUS_SUCCESS, ssoRole);
            } else {
                rs = result(MessageConstants.SSO_STATUS_FAIL);
            }
            // }
        } catch (Exception e) {
            e.printStackTrace();
            rs = setErrorJson(e.toString());
        }
        return rs;
    }

    /**
     * 删除角色
     *
     * @param oid
     * @return
     */
    @DeleteMapping(value = "/delete")
    public AjaxResult deleteEntity(String oid) {
        AjaxResult rs = null;
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("ROLE_OID", oid);
            // 查询角色是否已经被用户关联
            List<SsoRoleUser> list = ssoRoleUserService.listByMap(params);
            if (null != list && list.size() > 0) {
                rs = setJson(MessageConstants.SSO_STATUS_FAIL.getCode(), "角色已经被用户关联，无法删除！");
            } else {
                ssoRoleService.deleteByOid(oid);
                rs = result(MessageConstants.SSO_STATUS_SUCCESS);
            }
            // ssoRoleService.removeByIds(Arrays.asList(idList.split(",")));
        } catch (Exception e) {
            e.printStackTrace();
            rs = setErrorJson(e.toString());
        }
        return rs;
    }

    /**
     * 通过机构代码查询角色列表
     *
     * @param orgCode
     * @return
     */
    @RequestMapping(value = "/getDropDownList", method = RequestMethod.GET)

    public AjaxResult getDropDownList(String orgCode) {
        AjaxResult rs = null;
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("ORG_CODE", orgCode);
            List<SsoRole> list = ssoRoleService.listByMap(params);
            rs = json(MessageConstants.SSO_STATUS_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            rs = setErrorJson(e.toString());
        }
        return rs;
    }

}
